#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "struct.h"


// 查看窗口订单
void window_order_print(Window *pWin) {
	if (pWin->order_count == 0) {
		printf("当前窗口暂无订单！\n");
		return;
	}

	printf("\n     【%s订单列表】\n", pWin->window_name);
	printf("-----------------------------------\n");
	printf("|  序号    菜名     库存     单价 |\n");
	for(int i = 0; i < pWin->order_count; i++) {
		printf("|  %-4d   %-12s   %-6d   %-4.2f  |\n", 
				i + 1, pWin->Orders[i].name, pWin->Orders[i].stock, pWin->Orders[i].price);
	}
	printf("-----------------------------------\n");
}


// 7. 增加窗口订单
int Window_Order_add(Window *pWin) {
	
	if(NULL == pWin){
		printf("错误，窗口指针为空\n");
		return 0; 
	}
	
	//检查窗口是否营业 
	if(pWin->window_state!=1){
		printf("[%s]窗口已打烊，无法下单\n",pWin->window_name);
	}
	
	//检查是否有可点菜品
	if(pWin->dish_count<=0){
		printf("[%s]暂无菜品可点\n",pWin->window_name);
		return 0;
	}
	
	//检查订单是否已满 
	if(pWin->order_count>=MAX_DISH_PER_WINDOW){
		printf("菜品数量已达上限%d\n",MAX_DISH_PER_WINDOW);
		return 0;
	}
	
	//显示可点菜品（数组下标）
	printf("\n     [%s]可点菜品：\n",pWin->window_name) ;
    printf("-----------------------------------\n");
    printf("|  序号    菜名      价格     库存  |\n");
    for(int i = 0; i < pWin->dish_count; i++) {
        printf("|  %-4d   %-12s   %-6.2f   %-4d  |\n",
               i + 1, pWin->dishes[i].name, pWin->dishes[i].price, pWin->dishes[i].stock);
    }
    printf("-----------------------------------\n");
	
	
	 int dish_idx;  //输入订购菜品下标1-50
	 printf("请输入订购的菜品序号（1-%d）：", pWin->dish_count); // 1-50
	 if((scanf("%d",&dish_idx)!=1)||dish_idx<1||dish_idx>pWin->dish_count){
	 	printf("菜品 序号无效\n");
	 	while(getchar()!='\n');
	 	return 0;
	}
	 
	 //检查库存
	 if(pWin->dishes[dish_idx-1].stock<=0) {
	 	printf("[%s]已售罄\n",pWin->dishes[dish_idx-1].name);
	 	return 0;
	 }
	 
	//输入购买数量
	int buy_count;
	printf("输入购买数量（1-%d）:",pWin->dishes[dish_idx-1].stock);
	if(scanf("%d",&buy_count)!=1 || buy_count<=0 ||
	    buy_count > pWin->dishes[dish_idx-1].stock){
		printf("错误：购买数量无效\n");
        while (getchar() != '\n');
        return 0;
	}
	
	 // 添加到订单（使用当前订单数量作为新订单的下标）
	 int order_idx = pWin ->order_count;
	 strcpy(pWin->Orders[order_idx].name,pWin->dishes[dish_idx-1].name);
	 pWin->Orders[order_idx].price=pWin->dishes[dish_idx-1].price;
	 pWin->Orders[order_idx].stock= buy_count;
	
	// 减少库存
	pWin->dishes[dish_idx-1].stock -=buy_count;
	pWin->order_count++;
	
	
	printf("成功添加订单：[%s] x %d 订单序号：%d\n",
	pWin->Orders[order_idx].name,buy_count, pWin->order_count);
	return 1;
}

// 8. 删除窗口订单
int window_delete_add(Window *pWin){
	if(NULL == pWin){
		printf("错误，窗口为空");
		return 0;
	}
	//检查是否有订单
	if(pWin->order_count<=0) {
		printf("错误：无订单可删除\n");
		return 0;
	}
	//显示当前订单（带数组下标）
	printf("\n     [%s]当前订单:\n",pWin->window_name);
    printf("-----------------------------------\n");
    printf("|  序号    菜名       数量      单价 |\n");
    for(int i = 0; i < pWin->order_count; i++) {
        printf("|  %-4d   %-12s   %-6d   %-4.2f  |\n",
               i + 1, pWin->Orders[i].name, pWin->Orders[i].stock, pWin->Orders[i].price);
    }
    printf("-----------------------------------\n");
	// 输入要删除的订单下标
	int del_idx; //1-50
	printf("请输入删除的订单序号(1-%d):",pWin->order_count);  //1-50
	if(scanf("%d", &del_idx) != 1 || del_idx < 1 || del_idx > pWin->order_count) { 
        printf("错误：订单序号无效\n");
        while (getchar() != '\n');
        return 0;
    }
    
    // 恢复库存（通过菜品名称查找原菜品下标）
    for (int i = 0; i < pWin->dish_count; i++) {
        if (strcmp(pWin->dishes[i].name, pWin->Orders[del_idx].name) == 0) {
            pWin->dishes[i].stock += pWin->Orders[del_idx].stock;
            break;
        }
    }
	 // 移除订单（将后续订单向前移动，覆盖要删除的订单）
	 for (int i = del_idx;i<pWin->order_count-1;i++){
	 	pWin->Orders[i] =pWin->Orders[i+1];
	 }
	 pWin->order_count--;
	 
	 printf("成功删除订单，剩余订单数量：%d\n",pWin->order_count);
	 return 0;
}
// 9. 修改窗口订单
int Window_update_add(Window *pWin){
	if(NULL == pWin){
		printf("错误，窗口为空");
		return 0;
	}
	//检测是否有订单 
	 if (pWin->order_count <= 0) {
        printf("错误：当前没有订单可修改\n");
        return 0;
    }
    // 显示当前订单（带数组下标）|  数量：%d  |  单价：%.2f\n",
    printf("\n     [%s]当前订单：\n", pWin->window_name);
    printf("-----------------------------------\n");
    printf("|  序号    菜名       数量      单价 |\n");
    for(int i = 0; i < pWin->order_count; i++) {
        printf("|  %-4d   %-12s   %-6d   %-4.2f  |\n",
               i + 1, pWin->Orders[i].name, pWin->Orders[i].stock, pWin->Orders[i].price);
    }
    printf("-----------------------------------\n");
    //输入要修改的下标
	int upd_idx;  // 1-50
	printf("请输入修改的订单序号(1-%d):",pWin->order_count);
	if (scanf("%d", &upd_idx) != 1 || upd_idx < 1 || upd_idx > pWin->order_count) {
        printf("错误：订单序号无效\n");
        while (getchar() != '\n');
        return 0;
    }
      // 查找对应菜品的下标（用于库存计算）
    int dish_idx = -1;
    for (int i = 0; i < pWin->dish_count; i++) {
        if (strcmp(pWin->dishes[i].name, pWin->Orders[upd_idx-1].name) == 0) {
            dish_idx = i;
            break;
        }
    }
    if (dish_idx == -1) {	
        printf("错误：对应菜品已下架，无法修改订单\n");
        return 0;
		}
    // 计算最大可调整数量（当前库存 + 已订数量）
    int max_available = pWin->dishes[dish_idx].stock + pWin->Orders[upd_idx-1].stock;
    int old_count = pWin->Orders[upd_idx-1].stock;

    // 输入新的购买数量
    int new_count;
    printf("请输入新的购买数量（1-%d）：", max_available);
    if (scanf("%d", &new_count) != 1 || new_count <= 0 || new_count > max_available) {
        printf("错误：数量无效（必须为1-%d）\n", max_available);
        while (getchar() != '\n');
        return 0;
    }

    // 更新库存和订单数量
    pWin->dishes[dish_idx].stock += (old_count - new_count);  // 库存变化 = 原数量 - 新数量
    pWin->Orders[upd_idx-1].stock = new_count;

    printf("成功修改订单 %d：[%s] 数量从 %d 调整为 %d\n",
           upd_idx, pWin->Orders[upd_idx-1].name, old_count, new_count);
    return 1;
}
